import java.util.Stack;



public class Orders {
	private Node root;
	
	
	public Orders(Node r) {
		root = r;
	}


	public void preOrder() {
		Node current = root;
		Stack<Node> stack = new Stack<Node>();
		System.out.println("Preorder: ");
		while (current != null || !stack.isEmpty() ) {
			if ( current != null ){
				System.out.println(current.getValue());
				stack.push(current);
				current = current.getLeft();
								
			}
			else {
				current = stack.pop();
				current = current.getRight();
			}	
		}		
	}
	
	public void inOrder() {
		Node current = root;
		Stack<Node> stack = new Stack<Node>();
		System.out.println("Inorder: ");
		while (current != null || !stack.isEmpty() ) {
			if ( current != null ){
				stack.push(current);
				current = current.getLeft();
								
			}
			else {
				current = stack.pop();
				System.out.println(current.getValue());
				current = current.getRight();
			}	
		}				
			
	}
	
	public void postOrder() {		
		Node current = root;
		Stack<Node> stack = new Stack<Node>();
		System.out.println("Postorder: ");		

		
		while(current != null || stack.size() != 0){
			if(current != null){
				stack.push(current);
				current = current.getLeft();
			}
			else{				
				current=stack.pop();				
				if(current.getVisited()){
					System.out.println(current.getValue());
					current.setVisited(false);	
					current = null;
				}
				else{	
					current.setVisited(true);
					stack.push(current);
					current=current.getRight();						
				}
			}
		}		
	}

}
